package br.senai.sc.util.security;

import java.util.Map;

import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.jboss.seam.faces.event.PostLoginEvent;
import org.jboss.seam.faces.event.PreNavigateEvent;
import org.jboss.seam.security.Identity;
import org.jboss.solder.logging.Logger;

/**
 * Descrição da classe
 * 
 * @author Fernando Marquardt &lt;fernando.marquardt@sc.senai.br&gt;
 * @since 24/02/2012
 * @version $Id: LoginListener.java 28 2012-03-07 20:48:20Z fernando.marquardt $
 */
public class LoginListener {

    private final transient Logger log = Logger.getLogger(LoginListener.class);

    @Inject
    private Event<PostLoginEvent> postLoginEvent;

    public void preNavigateEvent(@Observes PreNavigateEvent event) {
        log.debugf("PreNavigateEvent observed %s, %s", event.getOutcome(), event.getFromAction());

        if (Identity.RESPONSE_LOGIN_SUCCESS.equals(event.getOutcome()) && "identity.login".equals(event.getFromAction().substring(2, 16))) {
            FacesContext context = event.getContext();
            Map<String, Object> sessionMap = context.getExternalContext().getSessionMap();
            postLoginEvent.fire(new PostLoginEvent(context, sessionMap));
        }
    }
}